package cn.flying.cloud.task.core.batch.tokenizer;

import java.lang.reflect.Field;
import java.util.Arrays;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;

import cn.flying.cloud.task.core.exception.TaskException;


/**
 * @author: admin
 * @date: 2024年02月23日 11:52
 * @version: 1.0
 */
public class DefaultDelimitedLineTokenizer extends DelimitedLineTokenizer {

    private Class<T> mapperClass;

    public Class<T> getMapperClass() {
        return mapperClass;
    }

    public void setMapperClass(Class<T> mapperClass) {
        this.mapperClass = mapperClass;
        Field[] fields;
        try {
            fields = mapperClass.newInstance().getClass().getDeclaredFields();
        } catch (Exception e) {
            throw new TaskException("字段映射类型赋值失败", e);
        }
        String[] names = Arrays.stream(fields).map(f -> f.getName()).toArray(String[]::new);
        super.setNames(names);
    }
}
